﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ShanNiang.XkmWork.Services.Implementations;
using ShanNiang.XkmWork.Services.Interfaces;
using System.Threading;
using System.Threading.Tasks;

namespace ShanNiang.XkmWork.Avalonia.ViewModels;

public partial class MainWindowViewModel : ViewModelBase
{
    private readonly IScanerService _scanerService;

    public MainWindowViewModel()
    {
        _scanerService = new ScanerService();
        _scanerService.BarcodeScanned += ScanerService_BarcodeScanned;
        Greeting = "Welcome to Avalonia!";
    }
    [ObservableProperty]
    private string _greeting; 


    [RelayCommand]
    private async Task LoadedAsync(CancellationToken cancellationToken)
    {
        await _scanerService.StartAsync(cancellationToken);
    }
    [RelayCommand]
    public async Task UnLoadedAsync(CancellationToken cancellationToken)
    {
        await _scanerService.StopAsync(cancellationToken);
    }    
    private void ScanerService_BarcodeScanned(object? sender, Services.Events.CodeEventArges e)
    {
        Greeting=e.Code;
    }
}
